#include "./../../lvgl/lvgl.h"
#include "./../../lvgl/demos/lv_demos.h"
#include "./../../lv_drivers/display/fbdev.h"
#include "./../../lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/album.h"
#include "./../../inc/page_manager.h"

#define COL_NUM 7      // 固定3列
#define MARGIN 10      // 边缘间距
#define GAP 10         // 间距
#define APP_WIDTH 100  // 宽度
#define APP_HEIGHT 100 // 高度

char * app_names[] = {"album", "calculator", "draw", "setting", "music", "2048", "jingziqi"};

void apps_init(void)
{
    lv_obj_t * apps_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(apps_screen, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(apps_screen, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_pad_all(apps_screen, 0, LV_PART_MAIN);

    navigate_to(apps_screen);

    // lv_obj_t * label = lv_label_create(apps_screen);
    // lv_obj_set_pos(label, 0, 380);
    // lv_label_set_text(label, "New text");
    // lv_obj_set_style_text_color(label, lv_color_hex(BLACK), LV_PART_MAIN); // 文本颜色
    // LV_FONT_DECLARE(lv_font_simsun_16_cjk);
    // lv_obj_set_style_text_font(label, &lv_font_simsun_16_cjk, LV_PART_MAIN | LV_STATE_DEFAULT);
    // lv_obj_move_foreground(label);
    // lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 20);
    lv_obj_set_size(apps_screen, 800, 480);
    // lv_obj_clean(home_page);

    // LV_IMG_DECLARE(bg1);
    lv_obj_t * img = lv_img_create(apps_screen);

    lv_img_set_src(img, "S:res/img/car_bg1.png");
    lv_obj_set_size(img, 800, 480);

    show_taskbar(apps_screen);
    show_apps(apps_screen);
    show_status_bar(apps_screen);
}

void show_apps(lv_obj_t * parent)
{
    lv_obj_t * appCard = lv_obj_create(parent);
    lv_obj_set_size(appCard, 800, 360);
    lv_obj_set_pos(appCard, 0, 60);
    lv_obj_set_style_radius(appCard, 0, 0);
    // lv_obj_set_style_bg_color(appCard, lv_color_hex(0x00808080), LV_PART_MAIN);

    lv_obj_set_style_bg_opa(appCard, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(appCard, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(appCard, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    lv_obj_set_style_pad_all(appCard, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(appCard, 0, LV_PART_MAIN);
    int count = 7;
    if(count <= 0) return; // 无图片时直接返回

    // 计算行数：3列布局，向上取整（如4张图需要2行）
    int row = (count + 2) / 3; // 等价于 ceil(count/3)，避免使用浮点数

    // 遍历所有图片，按行列布局
    for(int i = 0; i < row; i++) {         // 行循环
        for(int j = 0; j < COL_NUM; j++) { // 列循环
            int index = i * COL_NUM + j;   // 当前图片索引
            if(index >= count) break;      // 超出图片总数时退出

            // 当前图片位置
            int x = MARGIN + j * (APP_WIDTH + GAP);
            int y = MARGIN + i * (APP_HEIGHT + GAP);

            // 创建图片容器
            lv_obj_t * img_container = lv_obj_create(appCard);
            lv_obj_set_size(img_container, APP_WIDTH, APP_HEIGHT);
            lv_obj_set_pos(img_container, x, y);
            lv_obj_set_style_radius(img_container, 6, LV_PART_MAIN);

            lv_obj_set_style_bg_opa(img_container, LV_OPA_0, LV_PART_MAIN); // 设置背景完全透明

            lv_obj_set_style_border_width(img_container, 0, LV_PART_MAIN); // 移除边框

            lv_obj_clear_flag(img_container, LV_OBJ_FLAG_SCROLLABLE);     // 不可滚动
            lv_obj_clear_flag(img_container, LV_OBJ_FLAG_PRESS_LOCK);     // 禁用按压锁定
            lv_obj_clear_flag(img_container, LV_OBJ_FLAG_GESTURE_BUBBLE); // 禁用手势冒泡

            // 创建图片
            lv_obj_t * img = lv_img_create(img_container);

            char path[256];
            sprintf(path, "S:res/ui/%s.png", app_names[index]);
            // printf("path: %s\n", path);
            lv_img_set_src(img, path);
            lv_obj_set_size(img, APP_WIDTH, APP_HEIGHT);
            lv_obj_set_style_radius(img, 6, LV_PART_MAIN);
            lv_obj_center(img);

            char goto_app[256];
            lv_obj_add_flag(img, LV_OBJ_FLAG_CLICKABLE);
            if(strcmp(app_names[index], "music") == 0) {
                lv_obj_add_event_cb(img, open_music, LV_EVENT_CLICKED, NULL);
            } else if(strcmp(app_names[index], "calculator") == 0) {
                lv_obj_add_event_cb(img, open_calculator, LV_EVENT_CLICKED, NULL);
            } else if(strcmp(app_names[index], "draw") == 0) {
                lv_obj_add_event_cb(img, open_draw, LV_EVENT_CLICKED, NULL);
            } else if(strcmp(app_names[index], "album") == 0) {
                lv_obj_add_event_cb(img, open_album, LV_EVENT_CLICKED, NULL);
            } else if(strcmp(app_names[index], "setting") == 0) {
                lv_obj_add_event_cb(img, open_setting, LV_EVENT_CLICKED, NULL);
            } else if(strcmp(app_names[index], "jingziqi") == 0) {
                lv_obj_add_event_cb(img, open_tic_tac_toe, LV_EVENT_CLICKED, NULL);
            } else if(strcmp(app_names[index], "2048") == 0) {
                lv_obj_add_event_cb(img, open_2048, LV_EVENT_CLICKED, NULL);
            }
        }
    }
}
